package com.cqust.wdm.emsboot.controller;

import com.cqust.wdm.emsboot.domain.Category;
import com.cqust.wdm.emsboot.service.ICategoryService;
import com.cqust.wdm.emsboot.util.AjaxResult;
import com.cqust.wdm.emsboot.util.BaseController;
import com.cqust.wdm.emsboot.util.ExcelUtil;
import com.cqust.wdm.emsboot.util.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 分类Controller
 *
 * @author 1

 */
@RestController
@RequestMapping("/emsboot/category")
public class CategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询分类信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }


    /**
     * 获取分类信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(categoryService.selectCategoryById(id));
    }

    @ApiOperation("excel导出")
    @GetMapping("/export")
    public AjaxResult export(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "商品类别");
    }

    /**
     * 新增分类信息
     */

    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除信息
     */

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }
}
